package com.ygp.pattern;

import lombok.extern.slf4j.Slf4j;

/**
 * <Description> <br>
 *
 * @author linmiaolai@sanyygp.com<br>
 * @version 1.0<br>
 * @date 2021/11/17 <br>
 */
@Slf4j(topic = "c.ThreadSignal")
public class ThreadSignal {
    final Object lock = new Object();
    boolean hasPrintA = false;

    public static void main(String[] args) {
        ThreadSignal threadSignal = new ThreadSignal();
        threadSignal.printA();
        threadSignal.printB();
    }

    private void printA() {
        new Thread(() -> {
            while (true) {

                System.out.println("A");
                synchronized (lock) {
                    hasPrintA = true;
                    lock.notify();
                }
            }


        }, "线程A").start();

    }

    private void printB() {
        new Thread(() -> {
            synchronized (lock) {
                while (!hasPrintA) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            System.out.println("B");
        }, "线程B").start();
    }

}
